package com.ruoyi.redtea.dualprevent.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainRegion;
import com.ruoyi.redtea.dualprevent.domain.vo.TrainAnaUnitAssessVo;
import com.ruoyi.redtea.dualprevent.domain.vo.TrainFourColorVo;
import com.ruoyi.redtea.dualprevent.domain.vo.TrainRegionVo;
import com.ruoyi.redtea.dualprevent.mapper.TrainRegionMapper;
import com.ruoyi.redtea.dualprevent.service.TrainAssessAnalyseUnitService;
import com.ruoyi.redtea.dualprevent.service.TrainRegionService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 区域表(TrainRegion)表服务实现类
 *
 * @author makejava
 * @since 2024-05-13 14:37:28
 */
@Service("trainRegionService")
public class TrainRegionServiceImpl implements TrainRegionService {
    @Resource
    private TrainRegionMapper trainRegionMapper;

    @Resource
    TrainAssessAnalyseUnitService trainAssessAnalyseUnitService;

    @Override
    public TrainRegionVo queryById(Integer id) {
        return trainRegionMapper.selectVoById(id);
    }

    @Override
    public Page<TrainRegionVo> queryByPage(TrainRegion trainRegion, PageRequest pageRequest) {
        return null;
    }

    @Override
    public List<TrainRegionVo> queryPageList(PageQuery pageQuery) {
        PageUtils.startPage();
        List<TrainRegion> trainRegions = trainRegionMapper.selectList();
        ArrayList<TrainRegionVo> trainRegionVos = new ArrayList<>();
        for (TrainRegion trainRegion: trainRegions) {
            TrainRegionVo trainRegionVo = BeanUtil.toBean(trainRegion, TrainRegionVo.class);
            trainRegionVos.add(trainRegionVo);
        }

        return trainRegionVos;
    }

    @Override
    public List<TrainRegionVo> queryList() {
        List<TrainRegion> trainRegions = trainRegionMapper.selectList();
        ArrayList<TrainRegionVo> trainRegionVos = new ArrayList<>();
        for (TrainRegion trainRegion: trainRegions) {
            TrainRegionVo trainRegionVo = BeanUtil.toBean(trainRegion, TrainRegionVo.class);
            trainRegionVos.add(trainRegionVo);
        }
        return trainRegionVos;
    }

    @Override
    public boolean insert(TrainRegion trainRegion) {
        trainRegionMapper.setDelFlag(trainRegion);
        int insert = trainRegionMapper.insert(trainRegion);
        if (insert == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 亦可用于风险四色图的固有风险评定结果更新
     *
     * @param trainRegion 实例对象
     * @return
     */
    @Override
    public boolean update(TrainRegion trainRegion) {
        /*List<TrainRegion> trainRegions = new ArrayList<>();
        trainRegions.add(trainRegion);
        trainRegionMapper.updateBatchById(trainRegions);*/
        trainRegionMapper.setDelFlag(trainRegion);
        int updateById = trainRegionMapper.updateById(trainRegion);
        if (updateById == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean deleteByIds(List<Long> ids) {
        trainRegionMapper.deleteBatchIds(ids);
        return false;
    }

    @Override
    public List<TrainFourColorVo> queryFour() {
        //分析单元清单 含动态风险
        List<TrainAnaUnitAssessVo> trainAnaUnitAssessVos = trainAssessAnalyseUnitService.queryAssessList();
        List<TrainFourColorVo> trainFourColorVos = trainRegionMapper.queryFourColor();
        HashMap<Integer, Integer> levelMap = new HashMap<>();
        for (TrainFourColorVo fourColor : trainFourColorVos) {
            for (int i = 0; i < trainAnaUnitAssessVos.size(); i++) {
                TrainAnaUnitAssessVo assessVo = trainAnaUnitAssessVos.get(i);
                if(assessVo.getRegionId()==fourColor.getRegionId()){
                    Integer regionId = assessVo.getRegionId();
                    Integer integer = levelMap.get(regionId);
                    if(integer==null)integer=0;
                    if(assessVo.getDynamicRiskLevel()>integer){
                        levelMap.put(assessVo.getRegionId(),assessVo.getDynamicRiskLevel());
                    }
                }
            }
            fourColor.setControllRiskLevel(levelMap.get(fourColor.getRegionId()));
        }
        return trainFourColorVos;
    }


}
